package com.pubinfo.passbook.data.controller.sync;

import com.alibaba.fastjson.JSONObject;
import com.pubinfo.passbook.common.config.ApplicationConfig;
import com.pubinfo.passbook.common.entity.TBankApplyReceipt;
import com.pubinfo.passbook.common.entity.TFileInfo;
import com.pubinfo.passbook.common.model.bo.bank.*;
import com.pubinfo.passbook.common.model.dto.financial.InvoiceResult;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TBankApplyReceiptService;
import com.pubinfo.passbook.data.service.BankService;
import com.pubinfo.passbook.data.service.TFileInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping(value = "bank")
@Api(tags = "银行回单管理")
public class BankController {

    @Autowired
    BankService bankService;
    @Autowired
    TFileInfoService tFileInfoService;
    @Autowired
    TBankApplyReceiptService tBankApplyReceiptService;

    @PostMapping("applyReceipt")
    @ApiOperation(value = "申请银行单")
    public JsonResult<BankResult<String>> applyReceipt(@RequestBody BankApplyReceipt bankApplyReceipt) throws IOException {
        return JsonResult.success(bankService.applyReceipt(bankApplyReceipt));
    }

    @PostMapping("batchApplyReceipt")
    @ApiOperation(value = "申请一段时间的银行回单时间格式yyyy-MM-dd")
    public JsonResult<Boolean> batchApplyReceipt(String startDate,String endDate) throws IOException, ParseException {
        return JsonResult.success(bankService.batchApplyReceipt(startDate,endDate));
    }

    @PostMapping("queryReceipt")
    @ApiOperation(value = "查询银行单据")
    public JsonResult<TBankApplyReceipt> queryReceipt(@RequestBody String bankId) throws IOException {
        return JsonResult.success(bankService.queryReceipt(bankId));
    }

    @PostMapping("saveBankFile")
    @ApiOperation(value = "下载单据")
    public JsonResult<List<TFileInfo>> saveBankFile(@RequestBody String bankId) throws Exception {
        return JsonResult.success(tFileInfoService.saveBankFile(tBankApplyReceiptService.getById(bankId)));
    }

    @PostMapping("syncBankQueryReceipt")
    @ApiOperation(value = "更新银行回单状态")
    public void syncBankQueryReceipt() throws Exception {
        bankService.syncBankQueryReceipt();
    }

    @PostMapping("test1")
    @ApiOperation(value = "定时任务测试1")
    public void test1() throws Exception {
        bankService.syncBankApplyReceipt();
    }
    @PostMapping("test2")
    @ApiOperation(value = "定时任务测试2")
    public void test2() throws Exception {
        bankService.syncBankQueryReceipt();
    }
}
